cap cd "/Users/ddgueorg/Dropbox/China_Consultation/JOP Final"
cap cd "C:\Users\ejm5\Dropbox\China_Consultation\JOP Final"
clear all 
set more off

*Code for Replication KPR Figure 9, Gueorguiev and Malesky Figure 1 * 

use figure9.dta, clear /*based on point estimate and CI bounds reported in KPR 2013 replication files titled "RandomCAResultswCI.csv" and "RandomNoCAResultswCI.csv" */
lab var collective "Collective Action=1"
label values collective CAP
label define CAP 0 "No Collective Action" 1 "Collective Action"


lab var criticize "Criticize=1"
label values criticize criticize
label define criticize 0 "Criticize State" 1 "Support State"

generate category=_n
lab var category "Category"
label values  category  category
label define  category 1 "Criticize State" 2 "Support State" 3 "Criticize State" 4 "Support State"



twoway (rcap hi low category if criticize==1, lpattern(solid)) (rcap hi low category if criticize==0, lpattern(dash)) ///
(scatter mean category if criticize==1, msymbol(circle) msize(small)) (scatter mean category if criticize==0, msymbol(circle) msize(small)), ///
xlabel(1(1)4, labels labsize(medsmall) labcolor(black) format(%-12s) valuelabel) scheme(s1mono) legend(off) xtitle("") ///
ytitle("Percent Censored", size(medium) margin(medium)) ylabel(0(.2)1) ///
xline(2.5, lpattern(dash) lwidth(medthin) lcolor(black)) xscale(range(.7 4.3)) ///
title("      Collective Action Events           Not Collective Action Events", size(medium)) 
graph export Figure1_Fig9.eps, replace 


/**********************************************************************************************************************************************/
*Code for Replication KPR Figure 4, Gueorguiev and Malesky Figure 2 * 
/**********************************************************************************************************************************************/
clear all 
use "events.dta", clear /* Based on KPR 2013 replication file titles "Events for Replication Dataset.xlsx" */
generate id=_n
order id

drop if censorship==.
tab category, gen(category)
generate consultation=1 if category7==1
replace consultation=0 if id==24
replace consultation=0 if id==33
replace consultation=0 if id==80
replace consultation=0 if id==66
replace consultation=0 if consultation==.

generate CAP=1 if category2==1|category3==1|category4==1
replace CAP=0 if CAP==.

lab var CAP "Collective Action=1"
label values CAP CAP
label define CAP 0 "No Collective Action" 1 "Collective Action"

lab var consultation "Consultation=1"
label values consultation consultation
label define consultation 0 "No Consultation" 1 "Consultation"

generate censorshipmagnitude_CAP=censorshipmagnitude if CAP==1
generate censorshipmagnitude_consult=censorshipmagnitude if consultation ==1
generate censorshipmagnitude_other=censorshipmagnitude if category=="policy" & consultation ==0
replace censorshipmagnitude_other=censorshipmagnitude if category=="news" & consultation ==0
egen combine=concat(category id)
generate label2=label
replace label2="" if CAP==0 &  consultation==0 
encode label2, gen(label3)
sum censorshipmagnitude censorshipmagnitude_CAP censorshipmagnitude_consult censorshipmagnitude_other


graph dot (asis) censorshipmagnitude_CAP  censorshipmagnitude_consult, ///
over(label3, sort(censorshipmagnitude) descending label(labsize(tiny))) ///
marker(1, msize(medsmall) msymbol(square)) marker(2, msize(medsmall) ///
msymbol(circle_hollow)) ///
legend(rows(3) size(vsmall) ring(0) position(5) label(1 "Involved Collective Action") label(2 "Involved Consultation")) ///
ndots(10) dots(mcolor(gs15)) scheme(s1mono) ///
yline(.0374, lcolor(gs5)  lwidth(medthick)) ///
yline(-.0249, lcolor(gs5) lpattern(shortdash) lwidth(medthick)) ///
ytitle("Censorship Magnitude", size(medium) margin(small)) ///
ylab(-.2(.05).8, labsize(vsmall)) ///
note ("Solid Line: Mean Censorship in Sample" "Dashed Line: Mean Censorship of Policy & News without Consultation", position(7) size(vsmall))  
graph export Figure2_Fig4.eps, replace 

/**********************************************************************************************************************************************/
*Code for replicating Gueorguiev and Malesky Table 2 * 
/**********************************************************************************************************************************************/
eststo: reg censorshipmagnitude consultation , robust
outreg2 using event, tdec(3) bdec(3) e(rmse) replace
eststo: reg censorshipmagnitude consultation CAP, robust
esttab using basic_regression_burst.tex, b(%9.3fc) label replace ///
r2 se star(* 0.10 ** 0.05) eqlabels("") title(Censorship and Consultation) ///
compress mtitles("Consultation" "CAP Control" ) order ()
eststo clear

/*********************************************/
*Code for Generating CI simulations in  Gueorguiev and Malesky Appendix Figure D1 * 
/*********************************************/
clear all 
set seed 1234
*
set obs 101
g p=(_n-1)/100 
g inverse=1-p
lab var inverse "Inverse Probability of Censorship"

/*Assuming Equal Assignment to one of four categories (Critical/Collective Action*/
display 2000/4
display 20000/4
display 200000/4

gen se_1000=sqrt((p*inverse)/250)
gen se_10000=sqrt((p*inverse)/2500)
gen se_100000=sqrt((p*inverse)/25000)
  
generate mean_censorship=p
generate hi_1000=mean_censorship+(se_1000*1.96)
generate low_1000=mean_censorship-(se_1000*1.96)
generate hi_10000=mean_censorship+(se_10000*1.96)
generate low_10000=mean_censorship-(se_10000*1.96)
generate hi_100000=mean_censorship+(se_100000*1.96)
generate low_100000=mean_censorship-(se_100000*1.96)

generate hi_kpr=.207
generate low_kpr=.076
generate kpr=.142

twoway (rcap hi_1000 low_1000 p, vertical lcolor(gs10)) (rcap hi_kpr low_kpr kpr, vertical lwidth(medthick) lcolor(maroon)), ylab(0(.1)1) ytitle("") xtitle("") ///
xline(.141, lcolor(ltblue) lwidth(vvvthick)) title("1,000 Posts") legend(off) scheme(s1mono) ///
note("n=250", ring(0) position(4) size(medsmall)) ytitle(95% CI when CAP=0 and Criticism=1) ///
legend(rows(1) size(vsmall) ring(0) label(1 "Implied by Design") label (2 "Reported in KPR Fig. 9")) name(ci_1000)

twoway (rcap hi_10000 low_10000 p, vertical lcolor(gs10)) (rcap hi_kpr low_kpr kpr, vertical lwidth(medthick) lcolor(maroon)), ylab(0(.1)1) ytitle("") xtitle("") ///
xline(.141, lcolor(ltblue) lwidth(vvvthick)) title("10,000 Posts") legend(off) scheme(s1mono) ///
note("n=2500", ring(0) position(4) size(medsmall)) name(ci_10000)

twoway (rcap hi_100000 low_100000 p, vertical lcolor(gs10)) (rcap hi_kpr low_kpr kpr, vertical lwidth(medthick) lcolor(maroon)), ylab(0(.1)1) ytitle("") xtitle("") ///
xline(.141, lcolor(ltblue) lwidth(vvvthick)) title("100,000 Posts") scheme(s1mono) ///
note("n=25,000", ring(0) position(4)  size(medsmall)) name(ci_100000)

grc1leg ci_1000 ci_10000 ci_100000, xcommon ycommon cols(3) scheme(s1mono) 
graph export "Figure_D1.eps", replace


/*********************************************/
*Code for Generating Z-test simulation in  Gueorguiev and Malesky Appendix Figure D2 * 
/*********************************************/

/* Assuming 2000 Observations, Refer to Appendix D1 and Table D1 for calculations and cross-referencing */
clear all
set seed 123
set more off
set obs 841 /* 841 would be the corresponding proportion of non-CAP related posts in an overall sample of 2000. From Table D1, Panel C */ 
g critical = 0
forval x=1/315{         /* From Table D1, Panel C */
qui replace critical=1 in `x'
}
g censored=rbinomial(1,.135) if critical==1  /* censorship propensity, adopted to converge to KPR Figure 9 */
replace censored=rbinomial(1,.13) if critical==0
gsort -critical censored
prtest censored, by(critical)
preserve
forval x=1/115 {
drop in `x'
prtest censored, by(critical)
scalar z_`x' = r(z)
}
restore
forval x=1/115 {
g z_`x' =z_`x'
}
keep z_*
keep in 1
xpose, clear varname
replace v1=-1*(v1)
g drop=_n
g null= 0.0364
label variable v1 "Z-statistic"
label variable drop "Dropped Posts"
twoway (scatter v1 drop, msize(tiny)), ylabel(#10) xlabel(#30, labsize(small)) ///
yline(1.96, lpattern(dash) lcolor(red)) ylabel(0 1 1.96 2.75 ,labsize(small) angle(horizontal)) ///
xline(97.5, lcolor(gray) lwidth(vthin)) note("N=2,000, n=841") name(z_test841)
graph export "Figure_D2.eps", replace
